package com.htu.courseService.courseContents.controller;

import com.htu.courseService.common.vo.R;
import com.htu.courseService.courseContents.entity.File;
import com.htu.courseService.courseContents.service.FileService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created By JNNarrator on 2025/1/12 09:33
 * email: jiangn09@chinatelecom.cn
 */
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {
	private final FileService fileService;
	@PostMapping("/upload")
	public R<?> upload(Integer directoryId,@RequestParam("file") MultipartFile file){
		fileService.uploadFile(file,directoryId);
		return R.success();
	}
	@GetMapping("/list")
	public R<List<File>> list(Integer directoryId){
		return R.success(fileService.list(directoryId));
	}
	@GetMapping("/preview/{fileId}")
	public void preview(HttpServletResponse response, @PathVariable Integer fileId){
		fileService.downloadFile(response,fileId);
	}
	@DeleteMapping("/delete")
	public R<?> delete(Integer fileId){
		fileService.deleteFile(fileId);
		return R.success();
	}
}
